package com.iot08.admin.controller;

import com.iot08.admin.controller.base.BaseController;
import com.iot08.admin.service.ChannelService;
import com.iot08.admin.service.page.PageableImpl;
import com.iot08.common.constant.Const;
import com.iot08.common.entity.admin.SysMenu;
import com.iot08.common.entity.admin.SysRole;
import com.iot08.common.entity.admin.SysUser;
import com.iot08.common.response.Response;
import com.iot08.admin.service.MenuService;
import com.iot08.admin.service.RoleService;
import com.iot08.admin.service.page.PageVO;
import com.iot08.common.utils.StringUtil;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @version 1.0
 * @Description: 角色管理控制器
 * @author: Rock
 * @time: 2020/8/20 9:50
 */
@Controller
@RequestMapping("role")
public class RoleController extends BaseController {

    @Resource
    private RoleService roleService;
    @Resource
    private MenuService menuService;
    @Resource
    private ChannelService channelService;


    /**
     * @param [page]
     * @Description: 管理页面列表
     * @time: 2020/8/20 10:06
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("list")
    public ModelAndView list(PageVO page) {
        ModelAndView mv = new ModelAndView();
        Page<SysRole> pages = roleService.list(page, this.getSessionUser());
        mv.addObject("roles", pages.getContent());
        if (pages.getPageable() instanceof PageableImpl) {
            PageableImpl pi = (PageableImpl) pages.getPageable();
            mv.addObject("page", pi.getPage());
        }
        mv.addObject("butSet", getMyBut(page.getMenuId()));
        mv.setViewName("role/role_list");
        return mv;
    }

    /**
     * @Description: 添加角色页面
     * @time: 2020/8/20 10:09
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("toAdd")
    public ModelAndView toAdd() {
        ModelAndView mv = new ModelAndView();
        List<SysMenu> menus = new ArrayList<>();
        SysUser user = this.getSessionUser();
        if (user != null) {
            if ("admin".equals(user.getAccount())) {
                menus = menuService.listAll();
            } else {
                menus = menuService.listByIds(user.getRole().getMenuIds());
            }
            mv.addObject("menus", menus);
            mv.addObject("channels", channelService.listAll(user));
            mv.addObject("user", user);
            mv.setViewName("role/role_add");
        }
        return mv;
    }

    /**
     * @Description: 保存角色
     * @time: 2020/8/17 17:29
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public ModelAndView add(SysRole role) {
        ModelAndView mv = new ModelAndView();
        SysUser user = this.getSessionUser();
        if (user != null) {
            role.setCreator(user.getId());
            roleService.save(role, user, this.getRemortIP());
            mv.addObject("msg", "success");
        } else {
            mv.addObject("msg", "failed");
        }
        mv.setViewName("save_result");
        return mv;
    }

    /**
     * @Description: 角色编辑
     * @time: 2020/8/19 15:49
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("toEdit")
    public ModelAndView toEdit(String id) {
        ModelAndView mv = new ModelAndView();
        List<SysMenu> menus = new ArrayList<>();
        SysUser user = this.getSessionUser();
        if (user != null) {
            if ("admin".equals(user.getAccount())) {
                menus = menuService.listAll();
            } else {
                menus = menuService.listByIds(user.getRole().getMenuIds());
            }
            mv.addObject("menus", menus);
            mv.addObject("channels", channelService.listAll(user));
            mv.addObject("user", user);
            mv.addObject("role", roleService.findById(id));
            mv.setViewName("role/role_edit");
        }
        return mv;
    }

    /**
     * @Description: 保存角色编辑
     * @time: 2020/8/17 17:29
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping(value = "edit", method = RequestMethod.POST)
    public ModelAndView edit(SysRole role) {
        ModelAndView mv = new ModelAndView();
        roleService.update(role, this.getSessionUser(), this.getRemortIP());
        mv.addObject("msg", "success");
        mv.setViewName("save_result");
        return mv;
    }

    /**
     * @Description: 删除角色
     * @time: 2020/8/19 15:53
     * @author: Rock
     * @version 1.0
     */
    @ResponseBody
    @RequestMapping("del")
    public Response<Object> del(String id) {
        return roleService.updateDel(id, this.getSessionUser(), this.getRemortIP());
    }

    /*@ResponseBody
    @RequestMapping("checkName")
    public Response<Object> checkName(@RequestParam(required = false)String id, String name){
        SysRole role = roleService.getByName(name);
        if(StringUtil.isBlank(id)){
            if(role != null){
                return Response.failedResponse("角色名称已存在，请修改");
            }
        } else {
            if(role != null && !id.equals(role.getId())){
                return Response.failedResponse("角色名称已存在，请修改");
            }
        }
        return Response.successResponse();
    }*/
}
